<template>
  <div>
    <div class="pageheader">
      <XHeader :left-options="{backText:''}" :title="xheader.title" class="XHeader"></XHeader>
    </div>
    <div class="pagetop">
      <div class="flex flex-center topbar">
        <div class="flex-full inputbox flex flex-center block" @click="onSearchTap">
          <img class="icon" mode="aspectFit" src="@/assets/img/ic_search.png"/>
          <input disabled style="text-align: left" class="input flex-full" placeholder="搜索商品"/>
        </div>
      </div>

      <div class="toptab">
        <div
          v-for="(item,index) in tab"
          :key="index"
          class="item"
          :class="tabIndex==index?'active':''"
          @click="onTabTap(index)"
        >{{item}}折专区
        </div>
      </div>
    </div>
    <scroller
      @on-pullup-loading="getDiscountProductList"
      lock-x
      ref="scroller"
      :pullup-config="{content:'',upContent:'',downContent:'释放加载更多',loadingContent:'加载中...',}"
      height="-45"
      :use-pullup="true"
    >
      <div class="page">
        <div style="margin-top: 12px">
          <swiper
            class="swiper"
            :list="swipers"
            :showDescMask="false"
            dots-position="center"
            auto
            :height="'206px'"
          ></swiper>
        </div>
        <div class="block" v-if="products.length" style="margin-top: 24px">
          <!-- <div style="margin-bottom: 12px">{{tab[tabIndex]}}折专区</div> -->
          <div class="grid grid-gutter discountbox">
            <div class="grid-1-2" v-for="(product,productIndex) in products" :key="productIndex">
              <div class="item" @click="(onInfo(product))" data-product="product">
                <chimage class="image" :src="mainImages[product.goods_sku]" mode="aspectFill"></chimage>
                <div class="title">{{product.goods_name}}</div>
                <div class="price">{{product.internal_price}}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </scroller>
  </div>
</template>

<script>
  import Product from '@/dll/product'
  import Enum from '@/config/enum'
  import Adv from '@/dll/adv'

  export default {
    name: 'mallDiscount',
    data () {
      return {
        xheader: {
          title: '折扣'
        },
        tab: [],
        tabIndex: 0,
        productSourceType: 0,
        mainImages: [],
        products: [],
        swipers: [],
        limit: 20,
        page: 1,
        more: true
      }
    },
    created () {
      this.init()
    },
    methods: {
      init () {
        if (!Enum.PRODUCT_SOURCE_TYPE[this.$route.meta.id]) {
          this.$vux.alert.show({
            title: '提示',
            content: '活动已结束，亲，下次早点来哦',
            onHide: () => {
              this.$router.back()
            }
          })
          return
        }
        this.productSourceType = this.$route.meta.id
        this.xheader.title = Enum.PRODUCT_SOURCE_TYPE[this.$route.meta.id]
        this.getDiscountValueList()
        this.getDiscountSlideshow()
      },
      onSearchTap () {
        let so = {
          source: this.productSourceType
        }
        this.$router.push({
          name: 'mallSearch',
          query: { so: JSON.stringify(so) }
        })
      },
      onInfo (item) {
        this.$router.push({ name: 'mallInfo', params: { id: item.goods_sku } })
      },
      onTabTap (index) {
        if (this.tabIndex == index) {
          return
        }
        this.tabIndex = index
        this.products = []
        this.mainImages = []
        this.more = true
        this.page = 1
        this.$refs.scroller.enablePullup()
        this.$refs.scroller.reset({
          top: 0
        })
        this.getDiscountProductList()
      },
      // 获取折扣广告
      async getDiscountSlideshow () {
        let res = await Adv.getDiscountSlideshow()
        if (res.code == 0) {
          let swipers = res.data.map(item => {
            return {
              url: 'javascript:',
              img: item.adv_img
            }
          })
          this.swipers = swipers
        }
      },
      async getDiscountValueList () {
        this.$vux.loading.show({
          text: '加载中'
        })
        let res = await Product.getDiscountValueList()
        this.$vux.loading.hide()
        if (res.code == 0) {
          if (res.data[this.productSourceType]) {
            this.tab = res.data[this.productSourceType]
            this.getDiscountProductList()
          } else {
            this.$vux.alert.show({
              title: '提示',
              content: '活动已结束，亲，下次早点来哦',
              onHide: () => {
                this.$router.back()
              }
            })
          }
        } else {
          this.$vux.confirm.show({
            title: '提示',
            content: res.msg,
            showCancelButton: false,
            onConfirm: () => {
              this.$router.back()
            }
          })
        }
      },
      // 加载折扣商品
      async getDiscountProductList () {
        if (!this.more) {
          return
        }
        let param = {
          discount: this.tab[this.tabIndex],
          source: this.productSourceType,
          limit: this.limit,
          page: this.page
        }
        this.$vux.loading.show({
          text: '加载中'
        })
        let res = await Product.getDiscountProductList(param)
        this.$vux.loading.hide()
        if (res.code == 0) {
          if (res.data.data && res.data.data.length < this.limit) {
            this.more = false
            this.$refs.scroller.disablePullup()
          } else {
            this.$refs.scroller.donePullup()
          }
          this.page = this.page + 1
          this.products = [].concat(this.products, res.data.data)
          this.mainImages = Object.assign(this.mainImages, res.data.mainImage)
        } else {
          this.$refs.scroller.disablePullup()
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  .topbar {
    box-sizing: border-box;
    background-color: #fff;
    padding-top: 5px;
    padding-bottom: 5px;
  }

  .topbar .icon {
    width: 20px;
    height: 20px;
    padding-left: 12px;
    padding-right: 12px;
    display: block;
  }

  .topbar .inputbox {
    background: #fbf8f7;
    height: 35px;
  }

  .topbar .input {
    font-size: 12px;
    color: #351009;
    text-align: center;
    line-height: 17px;
  }

  .toptab {
    border-top: 1px solid #fbf8f7;
    box-sizing: border-box;
    width: 100%;
    display: flex;
    padding-left: 12px;
    padding-right: 12px;
    height: 45px;
    z-index: 9999;
    overflow: auto;
    background-color: #fff;
  }

  .toptab > .item {
    min-width: 25%;
    height: 100%;
    display: block;
    line-height: 45px;
    font-size: 14px;
    color: #9f8d89;
    text-align: center;
    position: relative;
  }

  .toptab > .item.active {
    color: #ae2309;
  }

  .toptab > .item.active::after {
    position: absolute;
    content: "";
    width: 100%;
    bottom: 0;
    left: 0;
    height: 1px;
    background: #b0290f;
  }

  .swiper {
    height: 206px;
    background-color: #fff;
  }

  .slide-image {
    height: 206px;
    width: 100%;
  }

  .discountbox .item {
    display: block;
    background-color: #fff;
    padding: 12px;
    box-sizing: border-box;
  }

  .discountbox .item > .image {
    display: block;
    height: 145px;
    width: 100%;
    background-color: #fbf8f7;
    background-size: cover;
  }

  .discountbox .item > .title {
    margin-top: 16px;
    font-size: 12px;
    color: #575757;
    line-height: 17px;
    height: 34px;
    text-overflow: ellipsis;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .discountbox .item > .price {
    font-size: 14px;
    color: #ae2309;
    line-height: 16px;
  }

  .page {
    padding-top: 90px;
  }

  .pagetop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 999;
  }
</style>
